﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.DAL
{
    public class MagUserDAL : BaseDAL<MagUserDAL>
    {
        #region 默认方法

        /// <summary>
        ///     增加一条数据
        /// </summary>
        public int Add(MagUser model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into MagUser(");
            strSql.Append(
                "UserName,Pwd,IsSuper,Mobile,RealName,RoleId,AddRoleMuduleId,RemoveRoleMuduleId,LastLoginIp,AddTime,IsDelete");
            strSql.Append(") values (");
            strSql.Append(
                "@UserName,@Pwd,@IsSuper,@Mobile,@RealName,@RoleId,@AddRoleMuduleId,@RemoveRoleMuduleId,@LastLoginIp,@AddTime,@IsDelete");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters =
            {
                new SqlParameter("@UserName", SqlDbType.NVarChar, 64),
                new SqlParameter("@Pwd", SqlDbType.NVarChar, 64),
                new SqlParameter("@IsSuper", SqlDbType.Int, 4),
                new SqlParameter("@Mobile", SqlDbType.NVarChar, 16),
                new SqlParameter("@RealName", SqlDbType.NVarChar, 32),
                new SqlParameter("@RoleId", SqlDbType.Int, 4),
                new SqlParameter("@AddRoleMuduleId", SqlDbType.NVarChar, 64),
                new SqlParameter("@RemoveRoleMuduleId", SqlDbType.NVarChar, 64),
                new SqlParameter("@LastLoginIp", SqlDbType.NVarChar, 16),
                new SqlParameter("@AddTime", SqlDbType.DateTime),
                new SqlParameter("@IsDelete", SqlDbType.Bit, 1)
            };

            parameters[0].Value = model.UserName;
            parameters[1].Value = model.Pwd;
            parameters[2].Value = model.IsSuper;
            parameters[3].Value = model.Mobile;
            parameters[4].Value = model.RealName;
            parameters[5].Value = model.RoleId;
            parameters[6].Value = model.AddRoleMuduleId;
            parameters[7].Value = model.RemoveRoleMuduleId;
            parameters[8].Value = model.LastLoginIp;
            parameters[9].Value = model.AddTime;
            parameters[10].Value = model.IsDelete;
            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        ///     更新一条数据
        /// </summary>
        public bool Update(MagUser model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update MagUser set ");

            strSql.Append(" UserName = @UserName , ");
            strSql.Append(" Pwd = @Pwd , ");
            strSql.Append(" IsSuper = @IsSuper , ");
            strSql.Append(" Mobile = @Mobile , ");
            strSql.Append(" RealName = @RealName , ");
            strSql.Append(" RoleId = @RoleId , ");
            strSql.Append(" AddRoleMuduleId = @AddRoleMuduleId , ");
            strSql.Append(" RemoveRoleMuduleId = @RemoveRoleMuduleId , ");
            strSql.Append(" LastLoginIp = @LastLoginIp , ");
            strSql.Append(" AddTime = @AddTime , ");
            strSql.Append(" IsDelete = @IsDelete  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters =
            {
                new SqlParameter("@Id", SqlDbType.Int, 4),
                new SqlParameter("@UserName", SqlDbType.NVarChar, 64),
                new SqlParameter("@Pwd", SqlDbType.NVarChar, 64),
                new SqlParameter("@IsSuper", SqlDbType.Int, 4),
                new SqlParameter("@Mobile", SqlDbType.NVarChar, 16),
                new SqlParameter("@RealName", SqlDbType.NVarChar, 32),
                new SqlParameter("@RoleId", SqlDbType.Int, 4),
                new SqlParameter("@AddRoleMuduleId", SqlDbType.NVarChar, 64),
                new SqlParameter("@RemoveRoleMuduleId", SqlDbType.NVarChar, 64),
                new SqlParameter("@LastLoginIp", SqlDbType.NVarChar, 16),
                new SqlParameter("@AddTime", SqlDbType.DateTime),
                new SqlParameter("@IsDelete", SqlDbType.Bit, 1)
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.UserName;
            parameters[2].Value = model.Pwd;
            parameters[3].Value = model.IsSuper;
            parameters[4].Value = model.Mobile;
            parameters[5].Value = model.RealName;
            parameters[6].Value = model.RoleId;
            parameters[7].Value = model.AddRoleMuduleId;
            parameters[8].Value = model.RemoveRoleMuduleId;
            parameters[9].Value = model.LastLoginIp;
            parameters[10].Value = model.AddTime;
            parameters[11].Value = model.IsDelete;
            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        ///     删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("update MagUser set IsDelete=1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters =
            {
                new SqlParameter("@Id", SqlDbType.Int, 4)
            };
            parameters[0].Value = Id;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        ///     批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("update MagUser set IsDelete=1 ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
        }

        /// <summary>
        ///     得到一个对象实体
        /// </summary>
        public DataSet GetModel(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append(
                "select Id, UserName, Pwd, IsSuper, Mobile, RealName, RoleId, AddRoleMuduleId, RemoveRoleMuduleId, LastLoginIp, AddTime, IsDelete  ");
            strSql.Append("  from MagUser ");
            strSql.Append(" where Id=@Id  and IsDelete=0");
            SqlParameter[] parameters =
            {
                new SqlParameter("@Id", SqlDbType.Int, 4)
            };
            parameters[0].Value = Id;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        ///     获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM MagUser where IsDelete=0 ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion

        #region 扩展方法

        /// <summary>
        ///     根据账号得到一个对象实体
        /// </summary>
        /// <remarks>2016-7-8 万乐园 创建</remarks>
        public DataSet GetModel(string userName)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * from MagUser ");
            strSql.Append(" where [UserName]=@UserName and IsDelete=0");
            SqlParameter[] parameters =
            {
                new SqlParameter("@UserName", SqlDbType.NVarChar, 64)
            };
            parameters[0].Value = userName;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        ///     根据账号密码得到一个对象实体
        /// </summary>
        /// <remarks>2016-7-8 万乐园 创建</remarks>
        public DataSet GetModel(string userName, string pwd)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * from MagUser ");
            strSql.Append(" where [UserName]=@UserName and [Pwd]=@Pwd and IsDelete=0");
            SqlParameter[] parameters =
            {
                new SqlParameter("@UserName", SqlDbType.NVarChar, 64),
                new SqlParameter("@Pwd", SqlDbType.NVarChar, 64)
            };
            parameters[0].Value = userName;
            parameters[1].Value = pwd;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetModelByUserName(string userName, int userId)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * ");
            strSql.Append("FROM MagUser ");
            strSql.Append("WHERE UserName=@UserName AND Id!=@UserId AND IsDelete=0 ");
            SqlParameter[] parameters = {
                    new SqlParameter("@UserName", SqlDbType.NVarChar,50),
                    new SqlParameter("@UserId", SqlDbType.Int,4)
            };
            parameters[0].Value = userName;
            parameters[1].Value = userId;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetModelByMobile(string mobile, int userId)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * ");
            strSql.Append("FROM MagUser ");
            strSql.Append("WHERE Mobile=@Mobile AND Id!=@UserId AND IsSuper = 0 AND IsDelete=0 ");
            SqlParameter[] parameters = {
                    new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
                    new SqlParameter("@UserId", SqlDbType.Int,4)
            };
            parameters[0].Value = mobile;
            parameters[1].Value = userId;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        ///     获取分页数据
        /// </summary>
        public List<MagUserDto> GetPagingList(int pageIndex, int pageSize, string searchKeyWord, string colOrder,
            ref int totalCount)
        {
            var strWhere = " IsDelete = 0 ";
            if (!string.IsNullOrEmpty(searchKeyWord))
            {
                strWhere += " AND (UserName LIKE '%" + searchKeyWord + "%' OR Mobile LIKE '%" + searchKeyWord +
                            "%' OR RealName LIKE '%" + searchKeyWord + "%' OR RoleName LIKE '%" + searchKeyWord + "%') ";
            }
            const string table =
                "(SELECT u.Id, u.UserName, u.Pwd, u.Mobile, u.RealName, u.RoleId, r.Name AS RoleName, u.LastLoginIp, u.AddTime, u.IsDelete " +
                "FROM MagUser u " +
                "LEFT JOIN MagUserRole ur ON u.Id = ur.UserId " +
                "LEFT JOIN MagRoles r ON ur.RoleId = r.Id WHERE u.IsSuper = 0 ) n ";
            return GetSimpleListPage<MagUserDto>(pageIndex, pageSize, table, null, null, colOrder, strWhere,
                ref totalCount);
        }

        #endregion
    }
}